package com.example.service.impl;

import com.example.controller.request.LinkGroupAddRequest;
import com.example.controller.request.LinkGroupUpdateRequest;
import com.example.interceptor.LoginInterceptor;
import com.example.manager.LinkGroupManager;
import com.example.model.LinkGroupDO;
import com.example.mapper.LinkGroupMapper;
import com.example.service.LinkGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.utils.CommonUtil;
import com.example.utils.JsonData;
import com.example.vo.LinkGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yinxin
 * @since 2022-06-08
 */
@Service
@Slf4j
public class LinkGroupServiceImpl extends ServiceImpl<LinkGroupMapper, LinkGroupDO> implements LinkGroupService {

    @Autowired
    private LinkGroupManager linkGroupManager;

    @Override
    public int add(LinkGroupAddRequest addRequest) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setTitle(addRequest.getTitle());
        return linkGroupManager.add(linkGroupDO);
    }

    @Override
    public int del(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        return linkGroupManager.del(groupId, accountNo);
    }

    @Override
    public LinkGroupVO detail(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId, accountNo);
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> listAllGroup() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList = linkGroupManager.listAllGroup(accountNo);
        List<LinkGroupVO> linkGroupVOList = linkGroupDOList.stream().map(o -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(o, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());
        return linkGroupVOList;
    }

    @Override
    public int updateGroupById(LinkGroupUpdateRequest updateRequest) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(updateRequest.getTitle());
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setId(updateRequest.getId());

        return linkGroupManager.updateGroupById(linkGroupDO);
    }
}
